package org.ipower.examsystem.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.ipower.examsystem.dao.CatalogDao;
import org.ipower.examsystem.dao.MembershipCatalogDao;
import org.ipower.examsystem.domain.Catalog;
import org.ipower.examsystem.domain.MembershipCatalog;
import org.ipower.examsystem.service.MembershipCatalogInfo;
import org.ipower.examsystem.service.MembershipCatalogInfoCollection;

/**
 * 会员科目数据访问实现类。
 * @author jeasonyoung
 */
public class MembershipCatalogDaoImpl  extends ModuleGenericDaoImpl<MembershipCatalog> implements MembershipCatalogDao{
    private CatalogDao catalogDao = null;
    
    @Override
    public void setCatalogDao(CatalogDao dao) {
        this.catalogDao = dao;
    }
    
    @Override
    public MembershipCatalogInfoCollection createCatalogTree(String memberID) {
       MembershipCatalogInfoCollection collection = new MembershipCatalogInfoCollection();
        List<Catalog> catalogs = this.catalogDao.loadAll();
        if(catalogs != null && catalogs.size() > 0){
            List<Catalog> removes = new ArrayList<Catalog>();
            Map<String,MembershipCatalog> maps = null;
            if(memberID != null && !memberID.isEmpty()){
                maps = this.loadMembershipCatalogs(memberID);
            }
            for(int i = 0; i < catalogs.size(); i++){
                Catalog c = catalogs.get(i);
                if(c.getParentCatalogID() == null || c.getParentCatalogID().isEmpty()){
                     MembershipCatalogInfo data = new MembershipCatalogInfo();
                     data.setId(c.getCatalogID());
                     data.setText(c.getCatalogName());
                     data.setLeaf(true);
                     if(maps != null && maps.size() > 0){
                         MembershipCatalog mc = maps.get(c.getCatalogID());
                         if(mc != null){
                             data.setChecked(true);
                             data.setCreateTime(mc.getCreateTime());
                             data.setCycle(mc.getCycle());
                             maps.remove(c.getCatalogID());
                         }
                     }
                      collection.add(data);
                      removes.remove(c);
                }
            }
            if(collection.size() > 0){
                catalogs.removeAll(removes);
                for(int i = 0; i < collection.size(); i++){
                    this.createTree(collection.get(i), catalogs, maps);
                }
            }
        }
       return collection;
    }
    
    private void createTree(MembershipCatalogInfo parent, List<Catalog> catalogs,Map<String,MembershipCatalog> maps){
        if(parent != null){
            List<Catalog> childs = this.loadChilds(catalogs,parent.getId());
            if(childs != null && childs.size() > 0){
                catalogs.removeAll(childs);
                for(Catalog c:childs){
                    MembershipCatalogInfo data = new MembershipCatalogInfo();
                    data.setId(c.getCatalogID());
                    data.setText(c.getCatalogName());
                    data.setLeaf(true);
                    if(maps != null && maps.size() > 0){
                       MembershipCatalog mc = maps.get(c.getCatalogID());
                        if(mc != null){
                            data.setChecked(true);
                            data.setCreateTime(mc.getCreateTime());
                            data.setCycle(mc.getCycle());
                            maps.remove(c.getCatalogID());
                        }
                    }
                    this.createTree(data, catalogs, maps);                    
                    parent.setLeaf(false);
                    parent.setExpanded(true);
                    parent.getChildren().add(data);
                }
            }
        }
    }
    
    private List<Catalog> loadChilds(List<Catalog>catalogs,String parentCatalogID){
        List<Catalog> result = new ArrayList<Catalog>();
        if(catalogs != null && catalogs.size() > 0 && parentCatalogID != null && !parentCatalogID.isEmpty()){
            for(int i = 0; i < catalogs.size();i++){
                Catalog c = catalogs.get(i);
                if(c != null && c.getParentCatalogID().equalsIgnoreCase(parentCatalogID)){
                    result.add(c);
                }
            }
        }
        return result;
    }  
    
    private Map<String,MembershipCatalog> loadMembershipCatalogs(String memberID) { 
        Map<String,MembershipCatalog> result = new HashMap<String,MembershipCatalog>();
        if(memberID != null && !memberID.isEmpty()){
            List<MembershipCatalog> list = this.loadAll("MemberID", memberID);
            if(list != null && list.size() > 0){
                for(int i = 0; i < list.size(); i++){
                    MembershipCatalog data = list.get(i);
                    if(data != null){
                        result.put(data.getCatalogID(), data);
                    }
                }
            }
        }
        return result;
    }
}